<template>
  <div>
    <template v-if="currentType === 'customer'">
      <customer :userId="externalUserId"></customer>
    </template>
    <template v-else>
      <group :chatId="chatId"></group>
    </template>
  </div>
</template>
<script>
  import customer from './swarmsSOP/swarmsSOP.vue'
  import group from './Customers/Customers.vue'
  export default {
    name: 'sop',
    components: {
      customer,
      group
    },
    data() {
      return {
        currentType: 'customer',
        externalUserId: '',
        chatId: ''
      }
    },
    created() {
      this.init()
    },
    methods: {
      init() {
        wx.invoke('getContext', {}, (res) => {
          if (res.err_msg == 'getContext:ok') {
            let entry = res.entry //返回进入H5页面的入口类型，目前有normal、contact_profile、single_chat_tools、group_chat_tools
            if (!['single_chat_tools', 'group_chat_tools'].includes(entry)) {
              // this.$toast.clear()
              this.$toast('入口错误：' + entry)
              return
            }
            if (entry === 'group_chat_tools') {
              this.currentType = 'group'
              wx.invoke('getCurExternalChat', {}, (res) => {
                if (res.err_msg == 'getCurExternalChat:ok') {
                  this.chatId = res.chatId //返回当前外部群的群聊ID
                } else {
                  //错误处理
                  this.$dialog({ message: '进入失败：' + JSON.stringify(res) })
                }
                this.$toast.clear()
              })
            } else {
              wx.invoke('getCurExternalContact', {}, (res) => {
                if (res.err_msg == 'getCurExternalContact:ok') {
                  this.externalUserId = res.userId
                } else {
                  //错误处理
                  this.$dialog({ message: '进入失败：' + JSON.stringify(res) })
                }
                this.$toast.clear()
              })
            }
          } else {
            //错误处理
            this.$toast.clear()
            this.$dialog({ message: '进入失败：' + JSON.stringify(res) })
          }
        })
      }
    }
  }
</script>
